/**
 * @author Nicolas Lusa (lusan@usi.ch)
 */
package ch.usi.inf.pf2.gui;

import java.util.HashSet;
import java.util.Iterator;
import java.util.ArrayList;

import ch.usi.inf.pf2.circuit.Component;

public final class Selection implements Iterable<Component> {

    private HashSet<Component> shapes;
    private ArrayList<SelectionListener> listeners;
    
    
    public Selection() {
        shapes = new HashSet<Component>();
        listeners = new ArrayList<SelectionListener>();
    }
    
    public void clear() {
        shapes.clear();
        System.out.println("Selection.clear()");
        fireSelectionChanged();
    }
    
    public int getSize() {
        return shapes.size();
    }
    
    public void add(final Component shape) {
        shapes.add(shape);
        fireSelectionChanged();
    }
    
    public void remove(final Component shape) {
        shapes.remove(shape);
        fireSelectionChanged();
    }
    
    public Iterator<Component> iterator() {
        return shapes.iterator();
    }
    
    
    //--- listener management
    public void addSelectionListener(final SelectionListener li) {
        listeners.add(li);
    }
    
    private void fireSelectionChanged() {
        for (final SelectionListener li : listeners) {
            li.selectionChanged();
        }
    }
    
}

